package com.zp.qualitypractice.Dialog;

import android.app.DialogFragment;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.zp.qualitypractice.R;

/**
 * Created by izp on 2017/6/14.
 */

public class PopupDialogFragment extends DialogFragment{
    private static final String EXT_Y = "y value";//高度 key
    private static final String EXT_BAR = "isTranslucentDecor"; //是否处理沉浸状态栏 key
    private int y; //高度值
    private boolean isTranslucentDecor; //是否处理沉浸状态栏 值

    /**
     * @param y Y轴位置，dialog的高度
     * @return 返回一个不对状态栏做处理的Fragment
     */
    public static PopupDialogFragment getInstant(int y) {
        return getInstant(y, false);
    }

    /**
     * @param y Y轴位置，dialog的高度
     * @param isTranslucentDecor  是否对沉浸是状态栏做处理
     * @return 返回对应的Fragment
     */
    public static PopupDialogFragment getInstant(int y, boolean isTranslucentDecor) {
        PopupDialogFragment fragment = new PopupDialogFragment();
        Bundle ext = new Bundle();
        ext.putInt(EXT_Y, y);
        ext.putBoolean(EXT_BAR, isTranslucentDecor);
        fragment.setArguments(ext);
        return fragment;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.dialogFrag);
//        setCancelable(true);
        Bundle args = getArguments();
        if (args != null) {
            y = args.getInt(EXT_Y, 0);
            isTranslucentDecor = args.getBoolean(EXT_BAR, false);
        } else {
            y = 0;
            isTranslucentDecor = false;
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
//        getDialog().setCanceledOnTouchOutside(false);
        getDialog().setCanceledOnTouchOutside(true);
        View rootView = inflater.inflate(R.layout.dialog_text, container, false);
        //Do something
        final Window window = getDialog().getWindow();
        WindowManager.LayoutParams wlp ;
        if (null!=window){
            window.setBackgroundDrawableResource(android.R.color.transparent);
            window.getDecorView().setPadding(0, 0, 0, 0);
            wlp = window.getAttributes();
        }else {
            wlp= new WindowManager.LayoutParams();
        }
//        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        wlp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        wlp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
        if (isTranslucentDecor) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                wlp.flags |= WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
            }
        }
        wlp.gravity = Gravity.TOP;//必须为 TOP，否则定位不准确
        wlp.y = y;//配合 Gravity.TOP 才能准确定位
        if (window != null) {
            window.setAttributes(wlp);
        }
        //Debug info
        rootView.findViewById(R.id.dialogText).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "dialogFragment 响应了点击事件", Toast.LENGTH_SHORT).show();
            }
        });
//        setCancelable(true);
        return rootView;
    }
}
